package com.cybertron.am.wordpress.controller;

import com.cybertron.am.wordpress.Constants;
import com.cybertron.am.wordpress.service.SysService;
import com.ne.boot.common.entity.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/sys")
@Api(tags = "系统操作")
@Slf4j
public class SysController {
    @Autowired
    private SysService sysService;

    @PostMapping(value = "/add-website", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiOperation(value = "添加站点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "domain", value = "站点域名", paramType = "form", required = true, dataType = "String")
    })
    public Response addWebsite(@ApiIgnore String domain) {
        Constants.POOL.submit(() -> {
            try {
                sysService.addWebsite(domain);
            } catch (Throwable e) {
                log.error("添加站点错误", e);
            }
        });
        return Response.success("任务已提交请稍后查看");
    }

    @PostMapping(value = "/copy-website", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiOperation(value = "复制站点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "source", value = "源站点域名", paramType = "form", required = true, dataType = "String"),
            @ApiImplicitParam(name = "target", value = "新站点域名", paramType = "form", required = true, dataType = "String")
    })
    public Response copyWebsite(@ApiIgnore String source, @ApiIgnore String target) {
        Constants.POOL.submit(() -> {
            try {
                sysService.copyWebsite(source, target);
            } catch (Throwable e) {
                log.error("复制站点错误", e);
            }
        });
        return Response.success("任务已提交请稍后查看");
    }

    @PostMapping(value = "/tree", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiOperation(value = "分类树")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "seedDomain", value = "数据站域名", paramType = "form", required = true, dataType = "String"),
    })
    public String tree(@ApiIgnore String seedDomain) {
        return sysService.tree(seedDomain);
    }

    @PostMapping(value = "/push-data", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiOperation(value = "推送数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "seedDomain", value = "数据站域名", paramType = "form", required = true, dataType = "String"),
            @ApiImplicitParam(name = "seedCategoryId", value = "数据站分类", paramType = "form", required = true, dataType = "String"),
            @ApiImplicitParam(name = "websiteDomain", value = "导入站点域名", paramType = "form", required = true, dataType = "String")
    })
    public Response pushData(@ApiIgnore String seedDomain, @ApiIgnore String seedCategoryId, @ApiIgnore String websiteDomain) {
        Constants.POOL.submit(() -> {
            try {
                sysService.pushData(seedDomain, seedCategoryId, websiteDomain);
            } catch (Throwable e) {
                log.error("推送数据错误", e);
            }
        });
        return Response.success("任务已提交请稍后查看");
    }
}
